define(['jquery', 'bootstrap', 'frontend', 'template', 'form','echarts', 'bootstrap-datetimepicker', 'table'],
    function ($, undefined, Frontend, Template, Form,echarts,datetimepicker,Table) {
        Date.prototype.format = function (fmt) {
            var o = {
                "M+": this.getMonth() + 1, // 月份
                "d+": this.getDate(), // 日
                "h+": this.getHours(), // 小时
                "m+": this.getMinutes(), // 分
                "s+": this.getSeconds(), // 秒
                "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
                "S": this.getMilliseconds() // 毫秒
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        }



        var mychart = {
                chartid:"",
                myChart:null,
                dataset:null,
                init:function(control,chartid){
                    var that = this;
                    that.chartid = chartid;
                    that.myChart = echarts.init(document.getElementById(that.chartid));
                    that._getDataAndDraw();
                    $("#"+that.chartid+"select").change(function(){
                        that._drawchart(that.dataset[$(this).val()]);
                    });
                },
                _getDataAndDraw:function(){
                    var that = this;
                    Fast.api.ajax({
                        url: "csmwj/paperstatistic/index_ajax_trend2",
                        type: "post",
                        data:{
                            begin:$("#datetimepicker1_input").val(),
                            end:$("#datetimepicker2_input").val()
                        },
                    }, function (data, ret) {
                        that.dataset = data;
                        that._drawchart(that.dataset[$("#"+that.chartid+"select").val()]);
                        return false;
                    }, function (data, ret) {
                        return false;
                    });

                },
                _drawchart:function(dataset){
                    var that = this;
                    // var option = {
                    //     legend: {
                    //         data: ['用户数']
                    //     },
                    //     tooltip: {trigger: 'axis'},
                    //     dataset: {
                    //         // 提供一份数据。
                    //         // source: [
                    //         // ['product', '游客统计', '登录用户统计'],
                    //         // ['2020-03-12', 43.3, 85.8],
                    //         // ['2020-03-13', 83.1, 73.4],
                    //         // ['2020-03-14', 86.4, 65.2],
                    //         // ['2020-03-15', 72.4, 53.9]
                    //         // ]
                    //         source:dataset
                    //     },
                    //     xAxis: {
                    //         type: 'category',
                    //     },
                    //     yAxis: {
                    //         type: 'value'
                    //     },
                    //     series: [
                    //         {
                    //             type: 'line',
                    //             smooth: false
                    //         },
                    //         {
                    //             type: 'line',
                    //             smooth: false
                    //         }
                    //     ]
                    // };
                    var option = {
                        // 标题
                        title: {
                            text: '',
                            left: 'center'
                        },
                        // 提示框
                        tooltip: {
                            trigger: 'item',
                            formatter: '{a} <br/>{b}: {c} ({d}%)'
                        },
                        // 图例
                        legend: {
                            orient: 'vertical',
                            left: 'left',
                            data: ['第一季度', '第二季度', '第三季度', '第四季度']
                        },
                        // 系列数据
                        series: [
                            {
                                name: '销售占比',
                                type: 'pie',
                                radius: '50%',
                                data: [
                                    { value: 335, name: '第一季度' },
                                    { value: 310, name: '第二季度' },
                                    { value: 234, name: '第三季度' },
                                    { value: 135, name: '第四季度' }
                                ],
                                // 标签设置
                                label: {
                                    show: true,
                                    formatter: '{b}: {d}%'
                                }
                            }
                        ]
                    };


                    that.myChart.setOption(option);
                },
            };


        var Controller = {
            index:function(){

                var that = this;
                that._dateselect();
                mychart.init(that,'chart1');
                that._renderlastregister();

                $("#dateconfirm").click(function(){
                    mychart._getDataAndDraw();
                });

            },
            lastregister:function(){

            },
            _renderlastregister:function(){
                var src = $("#chart2iframe").attr("src");
                   $("#chart2iframe").attr("src",src);
                   var day1 = new Date();
                var s1 = day1.format("yyyy-MM-dd hh:mm:ss");
                   $("#refreshchart2time").text("统计截止时间："+s1);
            },
            _dateselect:function(){
                var that = this;
                var day1 = new Date();
                var s1 = day1.format("yyyy-MM-dd");
                var day2 = new Date();
                day2.setDate(day1.getDate() - 7);
                var s2 = day2.format("yyyy-MM-dd");
                that._core_bui_datetimepicker('#datetimepicker1',s2,function(val){
                });
                that._core_bui_datetimepicker('#datetimepicker2',s1,function(){
                });

            },
            _core_bui_datetimepicker:function(jquerydomkey,date,onchangeFunc){
                $(jquerydomkey).datetimepicker({
                    showClose:true,
                    format:'YYYY-MM-DD',
                }).on('dp.change',function (ev) {
                    var v = $(jquerydomkey).val();
                    onchangeFunc(v);
                });
                $(jquerydomkey+"_input").val(date);
            },


            answerlist: function () {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        index_url: 'csmwj/paperstatistic/answerlist' + location.search,
                        table: 'csmwj_paper',
                    }
                });

                Fast.config.openArea = ['100%','100%'];
                var table = $("#table");

                var columns = [];
                columns.push();
                for(var i=0,j=Config.columns.length;i<j;i++){
                    var column = Config.columns[i];
                    columns.push({field: 'f_'+column.field, title: column.title});
                }
                // columns.push({field: 'submitmethod', title: '提交方式'});
                // columns.push({field: 'usedsecond', title: '使用时间(秒)'});
                // columns.push({field: 'score', title: '成绩'});
                columns.push({field: 'createtime', title: '提交时间', formatter: Table.api.formatter.datetime});

                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    // pk: 'id',
                    sortName: 'weigh',
                    search:false,
                    operate: false,
                    commonSearch: false,
                    showExport: false,
                    showColumns: false,
                    showToggle: true,
                    columns: [columns
    //                    [
    //                        {checkbox: true},
    //                        {field: 'id', title: __('Id')},
    //                        {field: 'name', title: __('Name')},
    //                    ]
                    ]
                });

                // 为表格绑定事件
                Table.api.bindevent(table);
            },
            answerlist2: function () {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        index_url: 'csmwj/paperstatistic/answerlist2' + location.search,
                        table: 'csmwj_paper',
                    }
                });

                Fast.config.openArea = ['100%','100%'];
                var table = $("#table");

                var columns = [];
                columns.push();
                for(var i=0,j=Config.columns.length;i<j;i++){
                    var column = Config.columns[i];
                    columns.push({field: 'f_'+column.field, title: column.title});
                }

                columns.push({field: 'createtime', title: '提交时间', formatter: Table.api.formatter.datetime});
                columns.push({field: 'image', title: '现场照片',formatter:Table.api.formatter.images});
                // 初始化表格
                $.fn.bootstrapTable.locales[Table.defaults.locale]['formatShowingRows'] = function (pageFrom, pageTo, totalRows) {
                    return '';
                };


                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    // pk: 'id',
                    sortName: 'weigh',
                    search:false,
                    operate: false,
                    commonSearch: false,
                    showExport: false,
                    showColumns: false,
                    showToggle: true,
                    columns: [columns

                    ]
                });

                // 为表格绑定事件
                Table.api.bindevent(table);
            },
        };

        return Controller;
    });
